package com.wkk.nursinghome.controller;


import com.wkk.nursinghome.domain.Checkin;
import com.wkk.nursinghome.service.ICheckinService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class CheckinController {
    //注入Service对象
    @Resource
    private ICheckinService checkinService;
    //显示所有信息
    @GetMapping("/checkin")
    public String selectAll(Model model){
        //获取所有信息
        List<Checkin> checkins=checkinService.selectAll();
        //将获取的信息添加到model中
        model.addAttribute("cins",checkins);
        //设置返回的试图名称-->templates/checkin.html
        return "checkin";
    }
    //删除员工
    @DeleteMapping("/checkin/{id}")
    @ResponseBody
    public String deleteById(@PathVariable Integer id){
        //调用方法判断是否删除成功
        boolean idDeleteSuccess=checkinService.deleteCheckinById(id);
        if(idDeleteSuccess){
            return "success";
        }else {
            return "fail";
        }
    }
    /**
     * 显示修改信息页面
     * @return
     */
    @GetMapping("/checkin/{id}")
    public String getCheckinById(@PathVariable("id")Integer id, Model model) {
        Checkin checkin = checkinService.queryById(id);
        System.out.println(checkin);
        model.addAttribute("cin", checkin);
        return "updateCheckin";
    }

    //修改部门信息
    @PutMapping("/checkin")
    public String update(Checkin checkin){
        boolean ismodifysuccess=checkinService.updateCheckin(checkin);
        if(ismodifysuccess) {
            return "redirect:/checkin";
        }else{
            return "first";
        }
    }

    //显示新增部门信息页面
    @GetMapping("/addCin")
    public String addDept(){
        return "/addCheckin";
    }
    //增加部门信息
    @PutMapping("/addC")
    public String add(Checkin checkin){
        checkinService.addCheckin(checkin);
        return "redirect:/checkin";
    }

    //根据条件查询
    @GetMapping("/searchC")
    public String searchByCondition(Model model,Checkin checkin){
        List<Checkin> checkins= checkinService.selectByCondition(checkin);
        model.addAttribute("cins",checkins);
        return "/checkin";
    }
}
